#ifndef DBMANAGER_H
#define DBMANAGER_H

#include <QObject>
#include <QtSql>
#include <QFile>
#include "entity/ownerentity.h"
#include "entity/vehicleentity.h"
#include "entity/userentity.h"
#include "entity/workerentity.h"
#include "entity/sparepartentity.h"
#include "entity/repairentity.h"

class DBManager : public QObject
{
public:
    static DBManager * getGlobal();

    bool initDB();
    void insertOwner(OwnerEntity *);
    void insertVehicle(VehicleEntity *);
    void insertUser(UserEntity *);
    void insertWorker(WorkerEntity *);
    void insertSparePart(SparePartEntity *);
    void insertRepair(RepairEntity *);

    void allOwners(QSqlTableModel * model);
    void allSpareParts(QSqlTableModel * model);
    
protected:
    bool createOwnerTable();
    bool createVehicleTable();
    bool createUserTable();
    bool createWorkerTable();
    bool createSparePartTable();
    bool createRepairTable();
    bool createRepairSparePartTable();

    bool openDB();
    void closeDB();
    QSqlError getError();

private:
    DBManager(QObject *parent = 0);
    QSqlDatabase db;
    static DBManager * instance;
    bool initOk;
};

#endif // DBMANAGER_H
